bookname = ['雪落香杉树','人间失格','活着',"浮生六记"]
books = {1:['雪落香杉树','伽特森', 5, 0],2:['人间失格', '太宰治', 5, 10],3:['活着', '余华', 0, 10],4:["浮生六记", '沈复', 0, 10]}
lendinformation = []
register_students = []
import time
from two_test import fontcolor_change as font

class Bookmanage():

    whether_return = []

    def __init__(self,admin = '姐姐'):
        self.admin = admin

    def find(self,name):
        """
        根据用户传入的书籍名称判断书籍是否存在,
        如果存在, 返回该书籍对象;
        如果不存在, 返回None
        """
        if name in bookname:
             return name
        else:
            return  font.Error + "书籍%s不存在" %(name) + font.END

    def recordlenddate(self):
            stime1 = time.localtime()
            stime2 = time.strftime('%Y -%m -%d', stime1)
            # print(time.strftime('%Y', stime1))
            # print(time.strftime('%m', stime1))
            # print(time.strftime('%d', stime1))
            return '{}:借出书籍'.format(stime2)

    def chat_explain(self):
        lendnumber = input('看了一圈的您请问要借几本书 ?')
        lendnumber = int(lendnumber)
        while lendnumber > 0:
            code = input('请说出编号:')
            code = int(code)
            name = input('请说出您将要借的书籍:')
            book = self.find(name)
            if book:
                if books[code][3] > 0:
                    if name in books[code]:
                        dict_information = dict(zip(bookname, font.bookinformation))
                        print('小弟弟，{}跟你说哟:  这书{}'.format(self.admin,dict_information[name]))
                        # self.recordlenddate()
                        books[code][2] += 1
                        books[code][3] -= 1
                        print(font.Ok + "书籍借阅成功" + font.END)
                    else:
                        print(font.warning + "书籍名与编码不符"  + font.END)
                        lendnumber += 1
                else:
                    print(font.warning + "书籍%s库存不足，已被抢借一空" % (name) + font.END)
                    lendnumber += 1
            else:
                print(font.Error + "书籍%s不存在" % (name) + font.END)
            lendnumber = int(lendnumber)
            lendnumber -= 1

    def return_book(self):
        lendnumber = input('您借了几本书？：')
        lendnumber = int(lendnumber)
        returnnumber = input('您要还几本书：')
        returnnumber = int(returnnumber)
        while returnnumber > 0:
            name = input("归还书籍名称：").strip()
            code = input('书籍的编号')
            code = int(code)
            book = self.find(name)
            if book:
                if name in books[code]:
                    books[code][2] -= 1
                    books[code][3] += 1
                    print(font.Ok + "书籍归还成功" + font.END)
                    if lendnumber == returnnumber:
                        Bookmanage.whether_return.append(' v ，都已归还!')
                        books[code][2] -= 1
                        books[code][3] += 1
                        print(font.Ok + "书籍归还成功" + font.END)
                        break
                    else:
                        Bookmanage.whether_return.append(' ×，{}本已归还!:{}'.format(returnnumber, name))
                else:
                    print(font.warning + "书籍名与编码不匹配" + font.END)
                    lendnumber += 1
            else:
                print(font.Error + "书籍%s不存在" % (name) + font.END)
            returnnumber = int(returnnumber)
            returnnumber -= 1


    def lendbook_name(self):
        lendnumber = input('抱歉要登记重复的再问您一下要借几本书 ?')
        name = input('请把你将要借的书籍的详细信息发一下:')
        lendinformation.append(lendnumber)
        lendinformation.append(name)
        return lendinformation

    def isleapyear(self,year):
        return year%400==0 or (year % 100!=0 and year%4 ==0)

    def havedays(self,year,month):
        list31 = [1, 3, 5, 7, 8, 10, 12]
        list30 = [4, 6, 9, 11]
        if month in list31:
            return 31
        elif month in list30:
            return 30
        elif month == 2:
            if self.isleapyear(year):
                return 29
            else:
                return 28
        else:
            return 0

    def recordreturndate(self):
        day = input('最多借30天您借几天?')
        day = int(day)                                 # 6
        stime1 = time.localtime()
        stime_year = time.strftime('%Y',stime1)
        stime_year = int(stime_year)
        stime_month = time.strftime('%m',stime1)
        stime_month = int(stime_month)                  # 6
        stime_day = time.strftime('%d',stime1)
        stime_day = int(stime_day)                      # 27
        res = self.havedays(stime_year, stime_month)    #30
        day1 = stime_day+day
        if day1 < res:
            return  '应{}-{}返回书籍'.format(stime_month,day1)
        else:
            stime_month = stime_month+1
            stime_day1 = day1-res
            return '应{}-{}返回书籍'.format(stime_month,stime_day1)



class Students():

    def __init__(self,students = {}):
        self.students = students

    def add(self, sid,name, age, lendbookname, lenddate, returndate,isreturn='0'):
        self.students['sid'] = sid
        self.students['name'] = name
        self.students['age'] = age
        self.students['lendbookname'] = lendbookname
        self.students['lenddate'] = lenddate
        self.students['returndate'] = returndate
        self.students['还书情况'] = isreturn




    @classmethod
    def lendbook(self):
        print('借书')

    @classmethod
    def returnbook(self):
        print('还书')


def main():
    global register_students
    bm = Bookmanage()
    s1 = Students()
    print(font.Ok + "初始化书籍信息成功" + font.END)
    print("""
    书籍借阅与管理系统
    1).查询书籍信息
    2).借书
    3).查询学生情况
    4).还书
    5).退出系统
    请输入你的选择：""")

    while 1:
        a = input('请输入指令：')
        if a == '1':
            print("书籍信息显示".center(50, '*'))
            print('''
编号      书名      作者    borrowed|   库存
            ''')
            for i in books:
                print('{}       {} '.format(i,books[i]))

        elif a == '2':

            bm.chat_explain()
            lenddate = bm.recordlenddate()
            lendname = bm.lendbook_name()
            returndate = bm.recordreturndate()
            print('请将书交给管理员并进行信息登记')
            register_sid = input('请输入学号:')
            register_name = input('请输入姓名:')
            register_age = input('请输入年龄:')
            s1.add(register_sid,register_name,register_age,lendname,lenddate,returndate)
            register_students.append(s1.students)
            print(font.Ok + "登记完成，欢迎下次光临！" + font.END)

        elif a == '3':
            print('借书学生信息情况如下：')
            for i in register_students:
                print(i)


        elif a == '4':
            bm.return_book()
            s1.students['还书情况'] = bm.whether_return

        elif a == '5':
            print('已成功退出！')
            break

main()







